package com.codefox.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.codefox.entity.TbUser;
import com.codefox.service.TbUserService;
import com.codefox.util.PageUtil;

/**
 * @author xiaobiye
 * @author litao
 * @date 2018年9月28日21:34:00
 */
@Controller
@RequestMapping(value = { "front", "back" })
public class TbUserController {
	@Autowired
    TbUserService tbUserService;
	
	/**
	 * 返回所有用户的json
	 * @return json字符串
	 */
	@ResponseBody
    @RequestMapping(value="/userList",produces = "application/json;charset=UTF-8")
    public String list() {
        List<TbUser> list = tbUserService.userFind();
        String json = JSONObject.toJSONString(list);
        return json;
    }
	
	@ResponseBody
    @RequestMapping(value="/userDelete",produces = "application/json;charset=UTF-8")
    public String delete(@RequestParam("id")Integer id) {
        if (tbUserService.delete(id)>0) {
			return "Y";
		}
        return "N";
    }
	
	@ResponseBody
    @RequestMapping(value="/findById",produces = "application/json;charset=UTF-8")
    public String findById(int id) {
        TbUser tbUser = tbUserService.findById(id);
        String json = JSONObject.toJSONString(tbUser);
		return json;
        
    }
	
    @RequestMapping(value="/userUpdate",produces = "application/json;charset=UTF-8")
    public ModelAndView update(TbUser tbUser) {
        tbUserService.update(tbUser);
        return new ModelAndView("redirect:userInfo.html?#");
    }
	
	@ResponseBody
    @RequestMapping(value="/getUserPage",produces = "application/json;charset=UTF-8")
	public String getUserPage(@RequestParam("pageIndex") int pageIndex,@RequestParam("pageSize") int pageSize){
		PageUtil<TbUser> pageUtil = tbUserService.getUserPage(pageIndex, pageSize);
		return JSON.toJSONString(pageUtil);
	}
}
